/**
 * CopilotPopup配置接口
 */
export interface PopupConfig {
  instructions: string;
  labels: {
    title: string;
    initial: string;
    placeholder: string;
  };
  defaultOpen: boolean;
  clickOutsideToClose: boolean;
  hitEscapeToClose: boolean;
  hideStopButton: boolean;
  imageUploadsEnabled: boolean;
}

/**
 * 触发器配置接口
 */
export interface TriggerConfig {
  type: 'floating' | 'fixed' | 'custom' | 'auto';
  position: string;
  showButton: boolean;
  buttonText: string;
  hotkey: string;
  autoTrigger: {
    enabled: boolean;
    conditions: string[];
  };
}

/**
 * 隔离配置接口
 */
export interface IsolationConfig {
  shadowDOMMode: 'closed' | 'open';
  styleIsolation: boolean;
  eventIsolation: boolean;
  globalVariablePrefix: string;
}

/**
 * 安全配置接口
 */
export interface SecurityConfig {
  enableCSP: boolean;
  sanitizeInput: boolean;
  validateOrigin: boolean;
  maxExecutionTime: number;
}

interface SuggestionsConfig {
  /**
   * A prompt or instructions for the GPT to generate suggestions.
   */
  instructions: string;
  /**
   * The minimum number of suggestions to generate. Defaults to `1`.
   * @default 1
   */
  minSuggestions?: number;
  /**
   * The maximum number of suggestions to generate. Defaults to `3`.
   * @default 1
   */
  maxSuggestions?: number;
  /**
   * Whether the suggestions are available. Defaults to `enabled`.
   * @default enabled
   */
  available?: 'enabled' | 'disabled';
  /**
   * An optional class name to apply to the suggestions.
   */
  className?: string;
}

/**
 * 主配置接口
 */
export interface MamisCopilotConfig {
  publicApiKey: string;
  runtimeUrl: string;
  configUrl: string;
  popup: PopupConfig;
  suggestions: SuggestionsConfig;
  trigger: TriggerConfig;
  isolation: IsolationConfig;
  theme: 'light' | 'dark' | 'auto';
  width: number;
  height: number;
  zIndex: number;
  primaryColor: string;
  borderRadius: number;
  fontFamily: string;
  persistHistory: boolean;
  contextAware: boolean;
  enableAnalytics: boolean;
  security: SecurityConfig;

  // 事件回调
  onReady?: () => void;
  onMessage?: (message: unknown) => void;
  onPageAction?: (action: string, target: string, result: unknown) => void;
  onError?: (error: Error) => void;
}

/**
 * 默认配置
 */
export const defaultConfig: MamisCopilotConfig = {
  runtimeUrl: 'https://mamisai.com/copilot/v1/runtime',
  configUrl: 'https://mamisai.com/ruoyi/v1/config',
  publicApiKey: 'your-mamis-copilot-apikey',

  // CopilotPopup配置
  popup: {
    instructions:
      '你是一个专业的网页操作助手，帮助用户分析、读取和操作 HTML 页面。用户所有问题都是针对当前 HTML 页面内容的，请首先分析页面内容和结构，然后再回答用户问题。',
    labels: {
      title: '智能助手',
      initial: '有什么可以帮助您的吗？',
      placeholder: '输入消息...',
    },
    defaultOpen: false,
    clickOutsideToClose: false,
    hitEscapeToClose: true,
    hideStopButton: false,
    imageUploadsEnabled: false,
  },
  suggestions: {
    instructions: '读取当前页面结构和内容，生成建议问题。',
  },

  // 触发器配置
  trigger: {
    type: 'floating',
    position: 'bottom-right',
    showButton: true,
    buttonText: '💬',
    hotkey: 'Ctrl+Shift+C',
    autoTrigger: {
      enabled: false,
      conditions: ['page-load', 'form-focus', 'error-detected'],
    },
  },

  // 隔离配置
  isolation: {
    shadowDOMMode: 'closed',
    styleIsolation: true,
    eventIsolation: true,
    globalVariablePrefix: 'MamisCopilot_',
  },

  // UI配置
  theme: 'light',
  width: 400,
  height: 600,
  zIndex: 10000,

  // 样式配置
  primaryColor: '#007bff',
  borderRadius: 12,
  fontFamily: 'system-ui',

  // 行为配置
  persistHistory: true,
  contextAware: true,
  enableAnalytics: false,

  // 安全配置
  security: {
    enableCSP: true,
    sanitizeInput: true,
    validateOrigin: true,
    maxExecutionTime: 5000,
  },
};
